$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bbusinessinfo/queryList',
        datatype: "json",
        colModel: [
            {
                label: '课程id',
                name: 'id',
                index: 'id',
                align: "center",
                width: 50,
                key: true,
                formatter: function (value, options, rowData) {
                    return "<a onclick=vm.editInfo(" + value + ")>" + value + "</a>";
                }
            },
            {label: '公众号标识', name: 'gzhName', index: 'gzh_name', align: "center", width: 80},
            {label: '(讲师)', name: 'authorName', index: 'author_name', align: "center", width: 80},
            {
                label: '课程封面',
                name: 'cover',
                index: 'cover',
                align: "center",
                width: 100,
                sortable: false,
                editable: false,
                formatter: function (value, options, rowData) {
                    var url = "https://lktvtool.oss-cn-shenzhen.aliyuncs.com/icon/236A5FF0-52DE-4644-B66C-E9555C97693D.png";
                    if (value == null || value == "") {
                        return "<img src='" + url + "' width='60' height='60'/>";
                    } else {
                        return "<img src='" + value + "' width='60'  height='60'/>";
                    }
                }
            },
            {label: '标题', name: 'title', index: 'title', align: "center", width: 80},
            {label: '内容', name: 'content', index: 'content', align: "center", width: 80},
            {label: '查看次数', name: 'lookCount', index: 'look_count', align: "center", width: 80},
            {label: '课程节数', name: 'numberCount', index: 'number_count', align: "center", width: 80},
            {
                label: '价格类型',
                name: 'rangePrice',
                index: 'range_price',
                align: "center",
                width: 80,
                formatter: function (value, options, rowData) {
                    var content = "";
                    if (value === 1) {
                        content = "<p style='color: black'>" + '全员免费' + "</>"
                    } else if (value === 2) {
                        content = "<p style='color: blue'>" + '会员免费' + "</>"
                    } else if (value === 3) {
                        content = "<p style='color: red'>" + '会员和非会员价格不同' + "</>"
                    }
                    return content;
                }
            },
            {label: '原价', name: 'originalPrice', index: 'original_price', align: "center", width: 80},
            {label: '非会员价', name: 'nonMembersPrice', index: 'non_members_price', align: "center", width: 80},
            {label: '会员价', name: 'membersPrice', index: 'members_price', align: "center", width: 80},
            {
                label: '课程类型', name: 'type', index: 'type', align: "center", width: 80,
                formatter: function (value, options, rowData) {
                    var content = "";

                    //课程类型
                    var bBusinessDictEntity = {
                        type: "课程类型"
                    };
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: baseURL + 'sys/bbusinessdict/queryAll',
                        contentType: "application/json",
                        data: JSON.stringify(bBusinessDictEntity),
                        success: function (r) {
                            if (r.code == 0) {
                                for (var i = 0; i < r.bBusinessDict.length; i++) {
                                    if(value == r.bBusinessDict[i].number){
                                        content = "<p>" + r.bBusinessDict[i].name + "</>"
                                    }
                                }
                            }
                        }
                    });
                    return content;
                }
            },
            {
                label: '是否精选', name: 'choiceness', index: 'choiceness', align: "center", width: 80,
                formatter: function (value, options, rowData) {
                    var content = "";
                    if (value === 0) {
                        content = '否';
                    } else if (value === 1) {
                        content = "是"
                    }
                    return content;
                }
            },
            {
                label: '删除状态',
                name: 'deleteState',
                index: 'delete_state',
                align: "center",
                width: 80,
                formatter: function (value, options, rowData) {
                    var content = "";
                    if (value === 1) {
                        content = '正常';
                    } else {
                        content = "<p style='color: red'>" + '删除' + "</>"
                    }
                    return content;
                }
            },
            {
                label: '操作', name: '', index: '', align: "center", width: 130,
                formatter: function (value, options, rowData) {
                    var status = rowData["deleteState"];
                    var id = rowData["id"];
                    var content = "";
                    if (status === 1) {
                        content = "<a style='text-align: center' class='btn btn-primary' onclick=vm.updateStatus('" + id + "',0)>" + "删除" + "</a>";
                    } else {
                        content = "<a style='text-align: center' class='btn btn-primary' onclick=vm.updateStatus('" + id + "',1)>" + "恢复" + "</a>";
                    }
                    content = content + "<a style='text-align: center' class='btn btn-primary' onclick=vm.editInfo('" + id + "',1)>" + "修改" + "</a>";
                    return content;
                }
            }
        ],
        viewrecords: true,
        height: 520,
        rowNum: 15,
        rowList: [15, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: '商学院课程信息',
        bBusinessInfo: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bBusinessInfo = {
                gzhCode: -1,
                rangePrice: 1,
                type: -1,
                choiceness: 0,
                deleteState: 1,
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bBusinessInfo.id == null ? "sys/bbusinessinfo/save" : "sys/bbusinessinfo/update";
            if(vm.bBusinessInfo.authorName == "" || vm.bBusinessInfo.authorName == null){
                alert("请选择课程讲师");
                return;
            }
            if(vm.bBusinessInfo.title == "" || vm.bBusinessInfo.title == null){
                alert("请输入完整课程标题");
                return;
            }
            if(vm.bBusinessInfo.content == "" || vm.bBusinessInfo.content == null){
                alert("请输入完整课程内容");
                return;
            }
            if(vm.bBusinessInfo.cover == "" || vm.bBusinessInfo.cover == null){
                alert("请上传课程封面");
                return;
            }
            if(vm.bBusinessInfo.type == "" || vm.bBusinessInfo.type == null){
                alert("请选择课程类型");
                return;
            }
            if(vm.bBusinessInfo.path == "" || vm.bBusinessInfo.path == null){
                alert("请上传小节音频");
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bBusinessInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/bbusinessinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/bbusinessinfo/info/" + id, function (r) {
                vm.bBusinessInfo = r.bBusinessInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.bBusinessInfo = {
                gzhCode: -1,
                rangePrice: 1,
                type: -1,
                choiceness: 0,
                deleteState: 1,
            };
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        editInfo: function (ids, event) {
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(ids)
        },
        updateStatus: function (ids, event) {
            vm.bBusinessInfo = {};
            confirm('确定修改状态？', function () {
                vm.bBusinessInfo.deleteState = event;
                vm.bBusinessInfo.id = ids;
                console.log(vm.bBusinessInfo)
                var url = "sys/bbusinessinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.bBusinessInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
    },

});
